package demo.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import demo.demo.CollectionSplitHelper.CollectionSplitter;
import demo.demo.CollectionSplitHelper.LimitChecker;
import demo.demo.RangeHelper.Range;
import demo.demo.RangeHelper.RangeBuilder;
import demo.demo.RowListHelper.ListRowsBuilder;
import demo.demo.RowListHelper.Row;

public class Demo {

	public static final int LIMIT_COUNT = 3;
	public static final int LIMIT_SIZE = 50;
	
	static List<CollectionSplitHelper.DemoFile> files = new ArrayList<CollectionSplitHelper.DemoFile>();
	static {
		files.add(new CollectionSplitHelper.DemoFile(25));
		files.add(new CollectionSplitHelper.DemoFile(5));
		files.add(new CollectionSplitHelper.DemoFile(10));
		files.add(new CollectionSplitHelper.DemoFile(30));
		files.add(new CollectionSplitHelper.DemoFile(10));
		files.add(new CollectionSplitHelper.DemoFile(20));
		files.add(new CollectionSplitHelper.DemoFile(5));
		files.add(new CollectionSplitHelper.DemoFile(10));
		files.add(new CollectionSplitHelper.DemoFile(30));
		files.add(new CollectionSplitHelper.DemoFile(5));
	}
	static List<CollectionSplitHelper.DemoFile> filesSmall = new ArrayList<CollectionSplitHelper.DemoFile>();
	static {
		filesSmall.add(new CollectionSplitHelper.DemoFile(15));
		filesSmall.add(new CollectionSplitHelper.DemoFile(5));
		filesSmall.add(new CollectionSplitHelper.DemoFile(10));
	}
	
	static List<CollectionSplitHelper.DemoFile> filesCantSplit = new ArrayList<CollectionSplitHelper.DemoFile>();
	static {
		filesCantSplit.add(new CollectionSplitHelper.DemoFile(95));
		filesCantSplit.add(new CollectionSplitHelper.DemoFile(10));
	}
	
	enum PositionType {
		LOADED, DIVIDER_AVAILABLE_TO_LOAD, DIVIDER_NOT_PURCHASED, AVAILABLE_TO_LOAD, NOT_PURCHASED;
	}
	
	public static void main(String[] args) {
//		demoLlIstRows();
//		demoRangeBuilder();
		demoSplitCollections();
	}

	private static void demoSplitCollections() {
		System.out.println("\n=== split big ===");
		split(LIMIT_COUNT, LIMIT_SIZE, files); 
		System.out.println("\n=== split small ===");
		split(LIMIT_COUNT, LIMIT_SIZE, files);
		System.out.println("\n=== cant split this ===");
		split(LIMIT_COUNT, LIMIT_SIZE, filesCantSplit);
	}

	private static void split(long limitCount, long sizeCount,
			List<CollectionSplitHelper.DemoFile> listToSplit) {
		System.out.println("Begin split. limit count:"+ limitCount + "; size count:"+sizeCount);
		System.out.println("List to split:");
		System.out.println("size:"+listToSplit.size());
		System.out.println("content:"+listToSplit.toString());
		LimitChecker<CollectionSplitHelper.DemoFile> checker1 = CollectionSplitHelper.createLimitChedcker(limitCount, sizeCount);
		
		List<List<CollectionSplitHelper.DemoFile>> splited = CollectionSplitter.split(listToSplit, checker1);
		
		System.out.println("After split:");
		int count = 1;
		for (List<CollectionSplitHelper.DemoFile> listDemoFiles : splited) {
			
			System.out.println(CollectionSplitHelper.generateTitle("Title", count, count+listDemoFiles.size()-1));
			count += +listDemoFiles.size();
			System.out.println(" bytes:"+ CollectionSplitHelper.getSize(listDemoFiles)
					+ " count:"+listDemoFiles.size()
					+ " content: "+listDemoFiles.toString());
		}
	}

	private static void demoRangeBuilder() {
		RangeBuilder<PositionType> rb = new RangeBuilder<PositionType>();
		rb.addRange(0, PositionType.LOADED);
		rb.addRangeWithDivider(2, PositionType.AVAILABLE_TO_LOAD, PositionType.DIVIDER_AVAILABLE_TO_LOAD);
		rb.addRangeWithDivider(0, PositionType.NOT_PURCHASED, PositionType.DIVIDER_NOT_PURCHASED);
		List<Range<PositionType>> ranges =  rb.getRanges();
		
		for (int position = 1; position < 10; position++) {
			Range<PositionType> range = rb.find(position);
			if (range != null)
				System.out.println(String.format("pos:" + position + "; type:"));
		}
	}

	private static void demoLlIstRows() {
		List<Integer> array1 = Arrays.asList(new Integer[] { 1,2,3,4,5,6,7 });
		List<Integer> array2 = Arrays.asList(new Integer[] { 11,22,33,44,55,66 });
		
		ListRowsBuilder<PositionType,Integer> b = new ListRowsBuilder<PositionType, Integer>();
		
		b.addRows(array1, PositionType.LOADED, 3);
		b.addRowsWithDivider(array2, PositionType.AVAILABLE_TO_LOAD, 3, PositionType.DIVIDER_AVAILABLE_TO_LOAD);
		b.addRowsWithDivider(array1, PositionType.NOT_PURCHASED, 2, PositionType.DIVIDER_NOT_PURCHASED);
		
		List<Row<PositionType, Integer>> rows = b.getRows();
		
		for (Row<PositionType, Integer> row : rows) {
			System.out.println(row);
			
		}
	}
	
}
